package me.monster.smarttraffic.ui.parking;

import java.util.Map;
import java.util.Random;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.monster.smarttraffic.base.BasePresenter;
import me.monster.smarttraffic.net.RemoteService;
import me.monster.smarttraffic.tool.StringUtil;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-12 20:31
 */
public class ParkingPresenter extends BasePresenter<ParkingContract.View> implements ParkingContract.Presenter {
    public static final int ERROR_NO_INPUT = 100;
    public static final int ERROR_PARKING_STATE = 200;
    public static final int ERROR_PARKING_PRICE = 300;
    public static final int ERROR_PARKING_PRICE_SET = 400;
    private static final String TAG = "ParkingPresenter";

    @Override
    public void fetchParkingState() {
        iView.showLoading();
        Disposable subscribe = RemoteService.getInstance().parkingState()
                .map(new Function<Map<String, Boolean>, Map<String, Boolean>>() {
                    @Override
                    public Map<String, Boolean> apply(Map<String, Boolean> stringBooleanMap) throws Exception {
                        Random random = new Random();
                        stringBooleanMap.put("parking1", random.nextInt() % 3 == 0);
                        stringBooleanMap.put("parking2", random.nextInt() % 4 == 0);
                        return stringBooleanMap;
                    }
                })
                .subscribe(new Consumer<Map<String, Boolean>>() {
                    @Override
                    public void accept(Map<String, Boolean> stringBooleanMap) throws Exception {
                        iView.dismissLoading();
                        iView.showParkingInfo(stringBooleanMap);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        handleError(throwable);
                        iView.showError(ERROR_PARKING_STATE);
                        iView.dismissLoading();
                    }
                });
        addDispose(subscribe);
    }

    @Override
    public void setParkingPrice(String parkingPrice) {
        if (StringUtil.isEmpty(parkingPrice)) {
            iView.showError(ERROR_NO_INPUT);
            return;
        }
        iView.showLoading();
        Disposable setDis = RemoteService.getInstance().setParkingPrice(parkingPrice)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        iView.dismissLoading();
                        iView.setPriceResult(true);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        handleError(throwable);
                        iView.dismissLoading();
                        iView.showError(ERROR_PARKING_PRICE_SET);
                    }
                });
        addDispose(setDis);
    }

    @Override
    public void fetchParkingPrice() {
        iView.dismissLoading();
        Disposable parkingPriceDis = RemoteService.getInstance().getParkingPrice().subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                iView.dismissLoading();
                iView.showParkingPrice(String.valueOf(new Random().nextInt(80)));
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                iView.dismissLoading();
                handleError(throwable);
                iView.showError(ERROR_PARKING_PRICE);
            }
        });
        addDispose(parkingPriceDis);
    }
}
